#include "shuerte.h"
#include "ui_shuerte.h"

#include<snakel.h>
#include <QTime>
#include <QMessageBox>
#include<shuertegamepanelwidget.h>
#include<shuertelevelselectwidget.h>
#include"ui_shuertelevelselectwidget.h"

shuerte::shuerte(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::shuerte)
{
    ui->setupUi(this);
    this->setObjectName("shuerte");
    this->setStyleSheet("#shuerte{border-image: url(:/image/图片/back.jpg);}");

    QPixmap img(":/image/图片/back.jpg");
            QPalette pal;
            pal.setBrush(this->backgroundRole(),QBrush(img));
            this->setPalette(pal);

    m_timer = new QTimer(this);
    m_timer->setInterval(1000);

        // 关联信号槽
        connect(m_timer, SIGNAL(timeout()), this, SLOT(SltTimeCount()));
        connect(ui->widgetGamePanel, SIGNAL(signalWin()), this, SLOT(SltWinTheGame()));
        //返回按钮
        re=new QPushButton("返回",this);
        re->setGeometry(129,580,131,48);
        re->setStyleSheet("QPushButton{font: 20 12pt '华文行楷 Regular';color: rgb(0,0,0);background-color: rgb(255,245,238);"
                                                                          "border: 2px groove gray;border-radius:20px;padding:2px 4px;border-style: outset;}"
                                                                          "QPushButton:hover{background-color: rgb(255,222,173);}"//hover
                                                                          "QPushButton:pressed{background-color: rgb(255,128,0);}"//pressed
                                                                            "border:2px solid rgb(20,196,188);");//边框粗细-颜色-圆角设置
        connect(re,SIGNAL(clicked()),this,SLOT(ensurere()));
        ui->btnStart->setStyleSheet("QPushButton{font: 20 12pt '华文行楷 Regular';color: rgb(0,0,0);background-color: rgb(255,245,238);"
                                    "border: 2px groove gray;border-radius:20px;padding:2px 4px;border-style: outset;}"
                                    "QPushButton:hover{background-color: rgb(255,222,173);}"//hover
                                    "QPushButton:pressed{background-color: rgb(255,128,0);}"//pressed
                                      "border:2px solid rgb(20,196,188);");//边框粗细-颜色-圆角设置
}

shuerte::~shuerte()
{
    delete ui;
}

void shuerte::on_btnStart_clicked()
{
    if (m_timer->isActive()) {
        m_timer->stop();
        ui->widgetGamePanel->Stop();
    }
    else {
        m_nTimeCnts = 0;
        m_timer->start();
        ui->widgetGamePanel->Start();
    }
}

void shuerte::SltTimeCount()
{
    m_nTimeCnts++;

    // 计算当前时分秒
    char nHour = (m_nTimeCnts / 3600);
    char nMin = (m_nTimeCnts % 3600) / 60;
    char nSec = (m_nTimeCnts % 3600) % 60;
    ui->labelTimes->setText(QString("%1:%2:%3").arg(nHour, 2, 10, QChar('0')).
                         arg(nMin, 2, 10, QChar('0')).
                         arg(nSec, 2, 10, QChar('0')));
}

void shuerte::SltWinTheGame()
{
    if (m_timer->isActive()) m_timer->stop();

    char nHour = (m_nTimeCnts / 3600);
    char nMin = (m_nTimeCnts % 3600) / 60;
    char nSec = (m_nTimeCnts % 3600) % 60;

    QString strTimes = QString("%1:%2:%3").arg(nHour, 2, 10, QChar('0')).
            arg(nMin, 2, 10, QChar('0')).
            arg(nSec, 2, 10, QChar('0'));

    QMessageBox::information(this, tr("恭喜你"), tr("恭喜你，你完成了游戏，用时[%1].").arg(strTimes));
    delete m_timer;
    m_timer=NULL;
}

void shuerte::on_btnLevelSelect_clicked()
{
    shuertelevelselectwidget *levelDialog = new shuertelevelselectwidget();
    levelDialog->exec();
    int nlevel = levelDialog->GetLevel();

    if (-1 != nlevel && nlevel < 4) {
        if (m_timer->isActive()) m_timer->stop();
        ui->widgetGamePanel->SetRows(3 + nlevel);
    }


}

void shuerte::ensurere()
{
    snakel *fater=new snakel();
    this->close();
    fater->show();
}

